package com.xiaobao.coalproperty.module.home.view;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.xiaobao.coalproperty.App;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.PriceEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.FormatUtils;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.xiaobao.coalproperty.other.widget.ChartView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

public class CoalDetailsActivity extends BaseActivity {

    @BindView(R.id.coal_start_time)
    TextView tvStartTime;
    @BindView(R.id.coal_end_time)
    TextView tvEndTime;
    @BindView(R.id.coal_chart)
    ChartView mChartView;

    private OptionsPickerView mOptionsPickerView;
    private boolean TYPE_START = true;
    private List<String> xData;
    private List<Float> yData;


    @Override
    protected int getContentView() {
        return R.layout.activity_coal_details;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("碳资产价格");

        tvStartTime.setOnClickListener(mOnClickListener);
        tvEndTime.setOnClickListener(mOnClickListener);

        mOptionsPickerView = new OptionsPickerView.Builder(this, mOnOptionsSelectListener).build();
        mOptionsPickerView.setPicker(App.getContext().getStartTimeList());
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.coal_start_time:
                    mOptionsPickerView.show();
                    TYPE_START = true;
                    break;
                case R.id.coal_end_time:
                    mOptionsPickerView.show();
                    TYPE_START = false;
                    break;
            }
        }
    };

    private OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener = new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int options2, int options3, View v) {
            if (TYPE_START) {
                tvStartTime.setText(App.getContext().getStartTimeList().get(options1));
            } else {
                tvEndTime.setText(App.getContext().getStartTimeList().get(options1));
            }
            updatePrice();
        }
    };

    private void updatePrice() {
        if (!checkDate()) {
            return;
        }
        OkHttpUtils.post().url(UrlConstant.URL_PRICE)
                .params(HttpHelperUtils.getInstance().priceParams(tvStartTime.getText().toString(), tvEndTime.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    PriceEntity entity = new Gson().fromJson(response, PriceEntity.class);
                    if (entity.getCode() == 0) {
                        updatePriceSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    private void updatePriceSuccess(PriceEntity entity) {
        xData = new ArrayList<>();
        yData = new ArrayList<>();
        for (int i = 0; i < entity.getData().getDate().size(); i++) {
            xData.add(entity.getData().getDate().get(i));
        }

        for (int i = 0; i < entity.getData().getPrice().size(); i++) {
            yData.add(Float.valueOf(entity.getData().getPrice().get(i)));
        }

        mChartView.setData(xData, yData);
    }

    private boolean checkDate() {
        try {
            FormatUtils.getInstance().formatDate(tvStartTime.getText().toString());
            FormatUtils.getInstance().formatDate(tvEndTime.getText().toString());
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
